//
//  AppController.m
//  CKstore
//
//  Created by liliang on 13-6-19.
//  Copyright (c) 2013年 李 良. All rights reserved.
//

#import "AppController.h"
#import "toolbox.h"
@implementation AppController

+(AppController*)shared{
    static AppController *singleton;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton=[[AppController alloc] init];
    });
    return singleton;
}

-(AppController*)init{
    self=[super init];
    if (self) {
        self.config=[NSMutableDictionary dictionaryWithContentsOfFile:@"config.xml".docPath];
        if (self.config==nil) {
            self.config=[NSMutableDictionary dictionary];
            [self saveSettings];
        }
    }
    return self;
}

-(void)saveSettings{
    [self.config writeToFile:@"config.xml".docPath atomically:YES];
}

-(BOOL)uploadFiles:(NSString *)filename
{
    BOOL noError = YES;
    AppController *app=[AppController shared];
    NSString *server=app.config[@"address"];
    if (server.length != 0) {
        server=[@"http://" stringByAppendingString:server];
    }
    NSString *uploader=[server stringByAppendingString:@"/ministore/upload.json"];
    @try {
        deviceIsBusy(YES);
        if (server.length == 0) {
            [NSException raise:LOC_STR(@"error") format:LOC_STR(@"未设置服务器"),nil];
            noError = NO;
        }
        else
        {
            NSString *fileaddress = [@"information".docPath stringByAppendingPathComponent:filename];
            NSData *data = [NSData dataWithContentsOfFile:fileaddress];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
            [request setURL:[NSURL URLWithString:uploader]];

            if (data == nil) {
                noError = NO;
            }
            else
            {

                NSString *str = [data.stringValue urlencode];
                NSString *string=[NSString stringWithFormat:@"body=%@",str];
                [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
                [request setHTTPMethod:@"POST"];
                [request setHTTPBody:string.dataValue];
                NSHTTPURLResponse *response = nil;
                NSError *error = nil;
                [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                if (error) {
                    noError = NO;
                }
                else
                {
                    if (response.statusCode == 200) {
                        [fileaddress removeFileOrDir];
                    }
                    else
                    {
                        NSDictionary *header=[response allHeaderFields];
                        NSString *msg=header[@"err_msg"];
                        NSLog(@"%@",msg.base64decode.stringValue);
                        noError = NO;
                    }
                }
            }

        }

    }
    @catch (NSException *exception) {

    }
    @finally {
        deviceIsBusy(NO);
    }
    return noError;
}

-(NSMutableArray *)getAllFiles:(NSArray *)files
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *filename in files) {
        if (![filename isEqualToString:@".DS_Store"]) {
            [array addObject:filename];
        }
    }
    return array;
}
@end
